import { layers as layerOptions } from "../config/mapStyles";
import { layersManager } from "./layerManager";
import { getGeoJsonNote } from '../tool/utils'

const rander = {
    // 行政区
    boundManager: {
        // 加载行政区
        boundaryLine(map: any, id: string, data: any) {
            layerOptions.fillLayerOptions.id = id + '_fill'  // 设置id
            layerOptions.fillLayerOptions.source.data = data // 设置数据
            layerOptions.fillLayerOptions.paint['fill-color'] = '#005bea' // 设置颜色
            layerOptions.fillLayerOptions.paint['fill-opacity'] = 0.5
            // 设置附加的properties
            layerOptions.fillLayerOptions.properties = { id: layerOptions.fillLayerOptions.id, remarks: '行政区面', delete: false }


            // 添加到图层
            layersManager.addLayer(map, layerOptions.fillLayerOptions)
            layerOptions.lineLayerOptions.id = id + '_line'  // 设置id
            layerOptions.lineLayerOptions.source.data = data // 设置数据
            layerOptions.lineLayerOptions.paint['line-color'] = '#f9d423' // 设置颜色
            layerOptions.lineLayerOptions.paint['line-width'] = 1 // 设置宽度
            // 设置附加的properties
            layerOptions.lineLayerOptions.properties = { id: layerOptions.lineLayerOptions.id, remarks: '行政区线', delete: false }
            // 添加到图层
            layersManager.addLayer(map, layerOptions.lineLayerOptions)

            const geojons = getGeoJsonNote(data.features)
            layerOptions.symbolLayerOptions.id = id + '_point'  // 设置id
            layerOptions.symbolLayerOptions.source.data = geojons
            layerOptions.symbolLayerOptions.paint['circle-color'] = '#bac8e0' // 设置颜色
            // 添加到图层
            layersManager.addLayer(map, layerOptions.symbolLayerOptions)

        },
        // 绘制注记
        async boundaryLabel(map: any, id: string, data: any) {
            let response = await fetch(data)
            // 解析 JSON 数据
            let jsonData = await response.json();
            for (let key in jsonData) {
                let value = jsonData[key]

                if (value.length >= 1) {
                    let newLayerOptions = { ...layerOptions.symbolLayerOptions };
                    newLayerOptions.source = {}
                    newLayerOptions.id = key  // 设置id
                    newLayerOptions.source['type'] = 'geojson' // 设置数据
                    newLayerOptions.source['data'] = getGeoJsonNote(value) // 设置数据
                    // 设置附加的properties
                    newLayerOptions.properties = { id: id + "_" + key, remarks: '注记', delete: false }

                    // 添加到图层
                    layersManager.addLayer(map, newLayerOptions)
                }
            }

        },
        // 移除行政区域边界线和注记
        removeBoundaryLine(map: any, id: string) {
            layersManager.removeLayer(map, id + "_fill")
            layersManager.removeLayer(map, id + "_line")
            layersManager.removeLayer(map, id + "_point")
        }
    },
    // 几何体
    geometryManager: {
        // 绘制线
        drawPolyline(map: any, id: string, coordinates: any) {
            let geoJson = {
                type: 'FeatureCollection',
                features: [
                    {
                        type: 'Feature',
                        geometry: {
                            type: 'LineString',
                            coordinates: []
                        },
                        properties: {}
                    }
                ]
            }
            let newAirOptions = { ...layerOptions.airLineOptions };
            newAirOptions.id = id  // 设置id
            newAirOptions.source = {}
            newAirOptions.source['type'] = 'geojson' // 设置数据
            newAirOptions.source['data'] = geoJson // 设置数据
            // 设置附加的properties
            newAirOptions.properties = { id: newAirOptions.id, remarks: '线', delete: true }
            // 添加到图层
            layersManager.addLayer(map, newAirOptions, false)
        },
        // 绘制点
        drawPoint(map: any, id: string, coordinates: any) {
            let geoJson = {
                type: 'FeatureCollection',
                features: [{
                    type: 'Feature',
                    geometry: {
                        type: 'Point',
                        coordinates
                    },
                    properties: {
                        bearing: 0
                    }
                }]
            }
            let newAirOptions = { ...layerOptions.airOptions };
            newAirOptions.id = id  // 设置id
            newAirOptions.source = {}
            newAirOptions.source['type'] = 'geojson' // 设置数据
            newAirOptions.source['data'] = geoJson // 设置数据
            // 设置附加的properties
            newAirOptions.properties = { id: newAirOptions.id, remarks: '点', delete: true }
            // 添加到图层
            layersManager.addLayer(map, newAirOptions, false)
        },
    }
}

export { rander }